Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

209
Visualizações
Why is my state array variable being treated as "pass-by-reference" and mutated when I use it as a parameter to another function?

I have a simple component here where I set a state variable called input to the value of an array of numbers. Then, within useEffect I call a function that randomizes the initial state array, and puts the results in a new state variable called output.

I need my input array to stay in the same order. However, it is being mutated when I call the shuffleArray function.

I thought there was no way to alter the value held by a variable passed as a parameter, which would be possible if JavaScript supported passing by reference.

const App = () => {
  const [input, setInput] = React.useState([90, 32, 28, 8, 21, 24, 64, 92, 45, 98, 22, 21, 6, 3, 27, 18, 11, 56, 16, 42, 36, 2, 60, 38, 24, 8, 16, 76, 62, 14, 84, 32, 24, 18, 8, 5, 25, 68, 65, 26, 22, 2, 52, 84, 30, 8, 2, 90, 5, 34, 56, 16, 42, 36]);

  const [output, setOutput] = React.useState([]);

  const shuffleArray = (array) => {
    for (let i = array.length - 1; i > 0; i--) {
        let j = Math.floor(Math.random() * (i + 1));
        let temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
    return array;
  }
  
  React.useEffect(() => {
      setOutput(shuffleArray(input));
  }, [])
  
  return (
    <div>
      [
       {
           input.length > 0 ?
               input.map((n, i) => (
                   <span key={i}>
                     { (i? ", " : "") + n }
                   </span>
               ))
               : 
               "No array..."
       }
      ]
    </div>
  );
};

ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script>

<div id="root"></div>

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The input is changed because that's the way pass-by-value works. Primitives cannot be mutated without reassignment. However, when it comes to objects (and arrays, as in this case), properties of the object can be mutated without reassignment.

If you want to keep input unchanged, you can use Array.from instead and manipulate a copy of the array.

const App = () => {
  const [input, setInput] = React.useState([90, 32, 28, 8, 21, 24, 64, 92, 45, 98, 22, 21, 6, 3, 27, 18, 11, 56, 16, 42, 36, 2, 60, 38, 24, 8, 16, 76, 62, 14, 84, 32, 24, 18, 8, 5, 25, 68, 65, 26, 22, 2, 52, 84, 30, 8, 2, 90, 5, 34, 56, 16, 42, 36]);

  const [output, setOutput] = React.useState([]);

  const shuffleArray = (array) => {
    const shuffled = Array.from(array);
    for (let i = shuffled.length - 1; i > 0; i--) {
        let j = Math.floor(Math.random() * (i + 1));
        let temp = shuffled[i];
        shuffled[i] = shuffled[j];
        shuffled[j] = temp;
    }
    return shuffled;
  }
  
  React.useEffect(() => {
      setOutput(shuffleArray(input));
  }, [])
  
  return (
    <div>
      [
       {
           input.length > 0 ?
               input.map((n, i) => (
                   <span key={i}>
                     { (i? ", " : "") + n }
                   </span>
               ))
               : 
               "No array..."
       }
      ]
    </div>
  );
};

ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script>

<div id="root"></div>

about 4 years ago · Juan Pablo Isaza Relatório

0

Javascript actually passes Objects and Arrays by "Copy of a Reference" as explained in this SO answer: https://stackoverflow.com/a/13104500/17704187

So your shuffleArray function actually mutates the contents of the input array.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda